package controllers

import java.util.UUID
import javax.inject.Inject

import models.Vessel
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json._
import play.api.mvc._
import play.modules.reactivemongo.{MongoController, ReactiveMongoComponents}
import services.{VesselsMongoService, VesselsService}

class VesselsController @Inject() (val myservice: VesselsMongoService)
    extends CRUDController[Vessel, UUID](myservice)(routes.VesselsController.read)
      {

  def search = Action.async(parse.json) { implicit request =>

    val limit = request.queryString.get("limit").getOrElse(DEFAULT_LIMIT).head.toInt

    validateAndThen[services.SearchQuery] {
      query =>
        myservice
          .search(query, limit)
          .map(
            entity => Ok(
              Json.toJson(entity)
            )
          )
    }
  }

}